local ClientOHOH =_G.ClientOHOH
local M = {}
local shopPanel = nil
local CallBackManager = ClientOHOH.load.CallBackManager()
local ConnectClass=_G.CommonOHOH.load.Connect();
local GeneralTool=_G.CommonOHOH.load.GeneralTool();
M.UseEvent=ConnectClass.new()
local LanguageHelper= _G.LanguageHelper
local UiData = {}
local UIManager = ClientOHOH.load.UIManager();
function Initsts()
    local ui = {
        list =  "ctrl.Potions.root",
        cell =  "ctrl.Potions.cell",
        gSideTag = "ctrl.SideBar.potions.btn",
        gSideTagPos = "ctrl.SideBar.potions.position"
    }
   
    return ui
end

function M.GetInitUiList()
    return Initsts()
end

function M.Init(ShopPanel)
    shopPanel = ShopPanel
    CallBackManager:AddRbxConnect(shopPanel,shopPanel.insts.potions.gSideTag.Activated,function()
        shopPanel:ScrollingGo(shopPanel.insts.potions.gSideTagPos)
    end)
    shopPanel.insts.potions.cell.Visible =false
end



function M.Update()
    for _,cell in pairs(UiData) do
        cell:update()
    end
end

function M.Full(data)
    for index, value in pairs(data) do
        local list = string.split(value.item_id,"_")
        M.FullPotionList(value,list[2])
    end
end
local cellStr = {
    buy1 = "ctrl.buy1.btn",
    buy1Num = "ctrl.buy1.num",
    buy1Price = "ctrl.buy1.price",
    buy10 = "ctrl.buy10.btn",
    buy10Num = "ctrl.buy10.num",
    buy10Price = "ctrl.buy10.price",
    duration = "ctrl.duration",
    grade = "ctrl.grade",
    icon = "ctrl.icon",
    name = "ctrl.name",
    description = "ctrl.description"
}

--创建药水cell
function M.CreateCell(data)
    local potionObj = shopPanel.insts.potions.cell:Clone()
    potionObj.Visible = true
    potionObj.Parent = shopPanel.insts.potions.list
    local potion = shopPanel:InitUiInst2(potionObj,cellStr)
    shopPanel:InitUiObj(potionObj)
    potion.buy1Num.Text = "x"..data.buy1.sellCount
    potion.buy1Price.Text = data.buy1.price
    potion.buy10Num.Text = "x"..data.buy10.sellCount
    potion.buy10Price.Text = data.buy10.price
    local duration = tonumber(data.buffInfo.time)
    local hours = math.floor(duration / 3600)
	local minutes = math.floor((duration % 3600) / 60)
	local seconds = duration - hours*3600 - minutes*60
    local time = ""
    if hours>0 then
        time = hours.."H"
    end
    if minutes>0 then
        time = time..minutes.."M"
    end
    if seconds>0 then
        time = time..seconds.."S"
    end
    potion.duration.Text =time

    potion.icon.Image = data.buffInfo.icon
    -- #todo:grade替换文字
    potion.grade.Text = "Normal"
    -- if data.buffInfo.grade==1 then
    --     potion.grade.Text = "Normal"
    -- elseif data.buffInfo.grade==2 then
    -- elseif data.buffInfo.grade==3 then
    -- elseif data.buffInfo.grade==4 then
    -- elseif data.buffInfo.grade==5 then
    -- end
    potion.name.Text = LanguageHelper:GetText(data.buffInfo.name)
    --#todo:之后会是有参文字
    potion.description.Text = LanguageHelper:GetText(data.buffInfo.description)

    CallBackManager:AddRbxConnect(shopPanel,potion.buy1.Activated,function()
        shopPanel.ShopBuyEvent:Action(data.buy1.buyInfo)
    end)
    CallBackManager:AddRbxConnect(shopPanel,potion.buy10.Activated,function()
        shopPanel.ShopBuyEvent:Action(data.buy10.buyInfo)
    end)
end

function M.FullPotionList(data,index)
    M.CreateCell(data)
end

function M.Get()
    return M
end

return M 